#include "animatedsprite.h"



AnimatedSprite::AnimatedSprite(const std::string &filename, unsigned nCols, unsigned nRows): TiledSprite (filename,nCols,nRows){
	
	frameTime=0;
	elapsedTime=0;
	isPlaying=false;

}


void AnimatedSprite::SetAnimation(const std::vector<unsigned> frames,float animationTime, bool loop, unsigned startFrame){

	this->frames=frames;//vector de frames
	frameTime=animationTime/frames.size();//El tiempo se divide por cada frame
	this->loop=loop;
	currentFrame=startFrame;
	isPlaying=true;

}




void AnimatedSprite::Animate(const float &dt){

	elapsedTime+=dt;
	if(isPlaying)
	{
		if(elapsedTime>=frameTime)
		{
	
			elapsedTime-=frameTime;
			currentFrame++;
	
			if(currentFrame>= frames.size())
			{//Si termina el vector
				if(loop)
				{
					currentFrame=0;
					SetTileIndex(frames[currentFrame]);
				}
				else
				{	isPlaying=false;}	
			}
			else
			{//Si no
				SetTileIndex(frames[currentFrame]);
			}


		}
	}
}


void AnimatedSprite::Stop(unsigned tileIndex){

	isPlaying=false;
	SetTileIndex(tileIndex);

}
